function ajaxRequest({
                         url = "",
                         async = true,
                         type = "POST",
                         data = null,
                         dataType = "json",
                         beforeSend = function () {
                         },
                         error = function () {
                         },
                         success = function () {

                         },
                         complete = function () {
                         },
                     }) {
    beforeSend();
    let xhr = createXmlHttpRequest();
    xhr.responseType = dataType;

    data = convertData(data);
    if (type.toUpperCase() === "GET") {
        url += ("?" + data);
        data = null;
    }

    xhr.open(type, url, async);

    if (type.toUpperCase() === "POST") {
        xhr.setRequestHeader('Content-Type', "application/x-www-form-urlencoded");
    }

    xhr.send(data);

    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                success(xhr.response);
            } else {
                error(xhr.response);
            }

            complete();
        }
    }
}

function createXmlHttpRequest() {
    if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }
}

function convertData(data) {
    if (typeof data === 'object') {
        let convertResult = "";
        for (let c in data) {
            convertResult += c + "=" + data[c] + "&";
        }

        convertResult = convertResult.substring(0, convertResult.length - 1);
        return convertResult;
    } else {
        return data;
    }
}